function cDate(nd) {
	// this is the symbol that separates months, days and years and you can set
	// it to anything, just pick your separator
	var separator = "/";

	// puts mm, dd and yyyy into an array
	var ndarr = nd.split(separator);

	// saves each argument of the array into a string starting with the day
	nd = ndarr[1] + separator + ndarr[0] + separator + ndarr[2];
	return nd;
}

function validateDates(){

var startDateText = document.getElementById('startDateCalender').value;
var endDateText = document.getElementById('endDateCalender').value;

var myList= startDateText.split("/");
var myList1= endDateText.split("/");

startDateText=myList[1]+"/"+myList[0]+"/"+myList[2];
endDateText=myList1[1]+"/"+myList1[0]+"/"+myList1[2];


var startDate= new Date(startDateText);
var endDate = new Date(endDateText);

var startDateMonth = startDate.getMonth()+1;
var endDateMonth = endDate.getMonth()+1;

  
if(startDate.getFullYear()>endDate.getFullYear()){
 
alert("Errors:\n"+"End Date must be greater than or equal to Start Date"); 
return false;
 
}else if(startDate.getFullYear()==endDate.getFullYear()){

if(startDateMonth>endDateMonth){
  alert("Errors:\n"+"End Date must be greater than or equal to Start Date"); 
  return false;
   
 }else if(startDateMonth==endDateMonth){
  if(startDate.getDate()>endDate.getDate()){
    alert("Errors:\n"+"End Date must be greater than or equal to Start Date"); 
    return false; 
   
    }else{
       return true;
     }


 }else{
    return true;
      
 }
  
}else{
 return true;
}

}

function validateFormBeforeSubmitForUpdateProjectOwner(theForm) {
	var reason = "";
	
	var startDateCalender = document.getElementById('startDateCalender').value;
	if(startDateCalender==""){
	alert("Start Date field has not been filled in.");
	fld.style.background = 'Yellow';
	return false;
	}
	
	if(!validateDates()){
	return false;
	}else{
	return true;
	}
	
}

function validateMe(fld, name) {
	var error = "";

	if (fld != null
			&& (fld.value == null || (fld.value != null && fld.value.length == 0))) {
		fld.style.background = 'Yellow';
		error = name + " field has not been filled in.\n";
	} else {
		var numaric = fld.value;
		for ( var j = 0; j < numaric.length; j++) {
			if (j == 0) {
				var alphaa = numaric.charAt(j);
				var chCode = alphaa.charCodeAt(0);
				if (chCode == "32") {
					fld.style.background = 'Yellow';
					error = name + " should not start with space.\n";
				}
			}
		}
		fld.style.background = 'White';
	}
	return error;
}

function compareProjectOwnerDates(startDateText, endDateText, projectStartDateText, projectEndDateText){
	var reason ="";
	if(startDateText != "") {
		var startDate= "";
		var endDate = "";
		var projectEndDate = "";
		var projectStartDate = "";
		
		if(projectEndDateText != "" && endDateText != "") {
			projectEndDate = cDate(projectEndDateText);
			endDate = cDate(endDateText);
			
			if(Date.parse(projectEndDate) < Date.parse(endDate)){
				alert("Errors:\n" +" Project Owner End date can not be after Project End date ("+projectEndDateText+").");
				document.getElementById('endDateCalender').value = projectEndDateText;
				return false;
				
			}
		} 
		if(projectEndDateText != "" && endDateText == "") {
			projectEndDate = cDate(projectEndDateText);
			document.getElementById('endDateCalender').value = projectEndDateText;
		} 
		if(projectStartDateText != "" && startDateText != "") {
			startDate= cDate(startDateText);
			projectStartDate = cDate(projectStartDateText);
			if(Date.parse(projectStartDate) > Date.parse(startDate)){
				alert("Errors:\n" +" Project Owner Start date can not be before Project Start date ("+projectStartDateText+").");
				document.getElementById('startDateCalender').value = projectStartDateText;
				return false;
			} 
		}
		
		return true;
	} else { 
		return true;
	}
}
